package leetcode.code2029;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution2 implements IDebug {

	boolean Alice = true;
	boolean Bob = false;

	public boolean stoneGameIX(int[] stones) {
		return this.stoneGameIX(stones, Alice, stones.length, 0);
	}

	private boolean stoneGameIX(int[] stones, boolean who, int end, int total) {
		if (end == 0) {
			return this.Bob;
		}

		for (int i = 0; i < end; i++) {
			int sum = stones[i] + total;
			if (sum % 3 != 0) {
				this.swap(stones, i, end - 1);
				if (this.stoneGameIX(stones, !who, end - 1, sum) == who) {
					return who;
				}
				this.swap(stones, i, end - 1);
			}
		}
		System.out.println(who + " " + end + !who);
		return !who;
	}

	private void swap(int[] stones, int i, int j) {
		int tmp = stones[i];
		stones[i] = stones[j];
		stones[j] = tmp;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(true, this.stoneGameIX(HelpDebug.str2array3("[2,1]")));
	}

	@Override
	public void debug2() {
		HelpDebug.compare(false, this.stoneGameIX(HelpDebug.str2array3("[2]")));
	}

	@Override
	public void debug3() {
		HelpDebug.compare(false, this.stoneGameIX(HelpDebug.str2array3("[5,1,2,4,3]")));
	}

	@Override
	public void debug4() {
		HelpDebug.compare(true, this.stoneGameIX(HelpDebug.str2array3("[2,2,1,2,3,4,5,6,1,1,1]")));

	}

	public void debug5() {
		HelpDebug.compare(true, this.stoneGameIX(HelpDebug.str2array3(
				"[77,74,12,63,95,23,19,91,48,87,26,22,21,30,41,10,22,80,14,36,62,29,13,3,15,47,71,1,95,21,43,84,62,70,10,86,70,9,38,30,51,32,75,87,73,8,54,64,35,22,68,75,4,59,69,82,27,9,20,32,64,59,58,48,32,21,15,20,75]")));

	}

	public static void main(String[] args) {
		Solution2 so = new Solution2();
//		so.debug1();
//		so.debug2();
//		so.debug3();
		so.debug4();
//		so.debug5();

	}

}
